/*
 * Copyright (C) 2017 Qi Cai Technology Co., Ltd. 
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at 
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and 
 * limitations under the License.
 */
package me.goldze.mvvmhabit.lks.dbmanager;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * Created by ww on 2017/7/19.
 */
public class LksSqliteOpenHelper extends OrmLiteSqliteOpenHelper {
    private static final String TABLE_NAME = "lqhy.db";
    private static final int VERSION = 1;
    private static LksSqliteOpenHelper instance;
    private Map<String, OrmDaoUtils> helperMap = new HashMap<>();

    public LksSqliteOpenHelper(Context context) {
        super(context, TABLE_NAME, null, VERSION);
    }

    /**
     * 单例获取该Helper
     *
     * @param context
     * @return
     */
    public static synchronized LksSqliteOpenHelper getHelper(Context context) {
        if (instance == null) {
            synchronized (LksSqliteOpenHelper.class) {
                if (instance == null)
                    instance = new LksSqliteOpenHelper(context);
            }
        }
        return instance;
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase, ConnectionSource connectionSource) {
        List<Class> classList = DBInitUtil.getClassList();
        if (null != classList && classList.size() > 0) {
            for (Class aClass : classList) {
                try {
                    TableUtils.createTable(connectionSource, aClass);
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, ConnectionSource connectionSource,
                          int i, int i1) {
        try {
            List<Class> classList = DBInitUtil.getClassList();
            Log.e("onUpgrade", "onUpgrade:>>>>>>>>>>size>>>>>>>>> "+ classList.size());
            if (null != classList && classList.size() > 0) {
                for (Class aClass : classList) {
                    try {
                        TableUtils.createTableIfNotExists(connectionSource, aClass);
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
            }

            List<UpDateHelperBean> beanList = DBInitUtil.getBeanList();
            if (null != beanList && beanList.size() > 0) {
                for (UpDateHelperBean upDateHelperBean : beanList) {
                    if (null == upDateHelperBean||upDateHelperBean.getVersion()<i1)
                        continue;
                    Log.e("onUpgrade", "onUpgrade:>>>>>>>>>>>>>>>>>>> "+ upDateHelperBean.getUpDateString());
                    getDao(upDateHelperBean.getaClass()).executeRaw(upDateHelperBean.getUpDateString());
                }
            }
//            Log.e("onUpgrade", "onUpgrade:>>>>>>>>>>>>>>>>>>>i "+ i);
//            Log.e("onUpgrade", "onUpgrade:>>>>>>>>>>>>>>>>>>>i1 "+ i1);
//            if(i<4){
//                List<Class> classList = DBInitUtil.getClassList();
//                if (null != classList && classList.size() > 0) {
//                    for (Class aClass : classList) {
//                        try {
//                            TableUtils.dropTable(connectionSource, aClass,true);
//                        } catch (SQLException e) {
//                            e.printStackTrace();
//                        }
//                    }
//                }
//                onCreate(sqLiteDatabase, connectionSource);
//            }

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public <T> OrmDaoUtils<T> getCustomDao(Context context, Class<T> aClass) throws SQLException {
        String className = aClass.getSimpleName();
        if (!helperMap.containsKey(className)) {
            helperMap.put(className, new DaoImp(context, aClass));
        }
        return helperMap.get(className);
    }

    @Override
    public void close() {
        super.close();
        helperMap.clear();
    }

}
